package dp.simple;

import com.sun.xml.internal.ws.addressing.WsaActionUtil;

/**
 * 斐波那契数
 * 示例 1：
 *
 * 输入：2
 * 输出：1
 * 解释：F(2) = F(1) + F(0) = 1 + 0 = 1
 * 示例 2：
 *
 * 输入：3
 * 输出：2
 * 解释：F(3) = F(2) + F(1) = 1 + 1 = 2
 * 示例 3：
 *
 * 输入：4
 * 输出：3
 * 解释：F(4) = F(3) + F(2) = 2 + 1 = 3
 * 链接：https://leetcode-cn.com/problems/fibonacci-number
 */
public class FibonacciNumber_509 {

    public int fib(int n) {

        int[] arr = new int[n + 1];
        arr[0] = 0;
        arr[1] = 1;
        for (int i = 2; i < n + 1; i++) {
            arr[i] = arr[i - 1] + arr[i - 2];
        }
        return arr[n];
    }

    public int fib_loop(int n) {
        if(n == 0)
            return 0;
        int a = 0, b = 1;
        int sum = a + b;
        int i = 2;
        while(i < n) {
            a = b;
            b = sum;
            sum = a + b;
            i++;
        }
        return sum;
    }


    public static void main(String[] args) {
        FibonacciNumber_509 fn = new FibonacciNumber_509();
        System.out.println(fn.fib(1900));
        System.out.println(fn.fib_loop(1900));
    }
}
